package com.windoent.yugioh.ui.activity.meinfo;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.mobstat.StatService;
import com.windoent.yugioh.MainActivity;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.GeneralBean;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.SimpleUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.ActivityTitle;
import com.windoent.yugioh.widget.PhoneCode;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import java.lang.ref.WeakReference;

import okhttp3.Call;


/**
 * author : robert
 * date   : 2021/5/10 10:15
 * desc   :店家验证码
 */
public class StoreAuthPhoneCodeActivity extends BaseActivity {
    private ActivityTitle mActivityTitle;
    private PhoneCode mPhoneCode;
    private LinearLayout mLlReload;
    private TextView mBtnGetCode;
    private String dianpu_phone;
    private String dianpu_id;

    @Override
    public int getLayout() {
        return R.layout.activity_store_auth_code;
    }

    @Override
    public void initView() {
        super.initView();

        mActivityTitle = (ActivityTitle) findViewById(R.id.activity_title);
        immersiveStatusBar(mActivityTitle.getView());
        mPhoneCode = (PhoneCode) findViewById(R.id.phone_code);
        mLlReload = (LinearLayout) findViewById(R.id.ll_reload);
        mBtnGetCode = (TextView) findViewById(R.id.btn_get_code);

    }

    @Override
    public void initData() {
        super.initData();
        Intent intent = getIntent();
        dianpu_id = intent.getStringExtra("dianpu_id");
        dianpu_phone = intent.getStringExtra("dianpu_phone");
        startTimer();
    }

    @Override
    public void initListener() {
        super.initListener();
        mActivityTitle.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mPhoneCode.setOnInputListener(new PhoneCode.OnInputListener() {
            @Override
            public void onSucess(String code) {
                //验证
                authDianjiaTwo(code);
            }

            @Override
            public void onInput(String phoneCode) {

            }
        });
        mLlReload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getCellPhoneCode(dianpu_phone);
            }
        });

    }

    //店家验证
    private void authDianjiaTwo(String code) {
        showLoadingDialog("");
        PhoneLive.authDianjiaTwo(getUserToken(), dianpu_id, dianpu_phone, code, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            startActivity(new Intent(mContext, MainActivity.class));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }

            }
        });
    }

    //发送验证码
    private void getCellPhoneCode(String phone) {
        showLoadingDialog("");
        PhoneLive.getCellPhoneCode(phone, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            startTimer();
                            GeneralBean generalBean = gson.fromJson(response, GeneralBean.class);
                            ToastUtils.showText(generalBean.getMsg());
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }

            }
        });
    }

    //倒计时
    private void startTimer() {
        mLlReload.setEnabled(false);
        SimpleUtils.startTimer(new WeakReference<TextView>(mBtnGetCode), "60s", 60, 1, new SimpleUtils.TimerListener() {
            @Override
            public void onFinished() {
                mLlReload.setEnabled(true);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "店家验证码界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "店家验证码界面");
    }
}
